<?php
/**
 * 统计图
 *
 * @author 
 * @version v1.0.0
 *
 **/
namespace Xijie\Controller;
use Think\Controller;

class ChartController extends Controller {
	public function __construct() {
        parent::__construct();
		//判断是否登陆
		if(session('user_id')){ //已登陆
			
		}else{ //未登陆，跳转道登陆界面
			$this->redirect("index/index");
		}
    }
	
	/**
	 * 房间费用规则
	 **/
	public function memberchart(){
		$year = date('Y', time());
		//查询租用率
		$memberGroup = M('member')
						->field("TIMESTAMPDIFF(month, FROM_UNIXTIME(btime,'%Y-%m-%d'), FROM_UNIXTIME(etime,'%Y-%m-%d')) as months, count(*) as counts")
						->where(array(
							//"FROM_UNIXTIME(btime,'%Y')" => date('Y'),
							'visible' => 1,
							'istype' => 1
						))->group("TIMESTAMPDIFF(month, FROM_UNIXTIME(btime,'%Y-%m-%d'), FROM_UNIXTIME(etime,'%Y-%m-%d'))")
						->select();
		//echo M('member')->_sql();
		$count1 = $count2 = $count3 = $count4 = 0;
		foreach($memberGroup as $ms){
			if($ms['months']/1 <= 3){
				$count1 += $ms['counts'];
			}else if($ms['months']/1 <= 6){
				$count2 += $ms['counts'];
			}else if($ms['months']/1 <= 9){
				$count3 += $ms['counts'];
			}else if($ms['months']/1 > 9){
				$count4 += $ms['counts'];
			}

			$counts += $ms['counts'];
		}
		
		$this->assign('count1', $count1);
		$this->assign('count2', $count2);
		$this->assign('count3', $count3);
		$this->assign('count4', $count4);
		//统计房型数量
		$houseList = M('room')->field('root_type, count(*) as counts')->where(array(
			'rent_type' => '长租'
		))->group('root_type')->order('root_type asc')->select();
		$houseArr = '';
		foreach($houseList as $ls){
			$houseArr .= strlen($houseArr) ? ','. json_encode($ls['counts']) : '['. json_encode($ls['counts']);
			$total += $ls['counts'];
		}
		$houseArr .= ']';
		
		//统计房型出租率
		$memberList = M('member')->alias('member')
			->join('left join momoda_room as room on member.room_number = room.room_number')
			->field('room.root_type, count(*) as counts')
			->where(array('member.visible' => 1))
			->group('room.root_type')
			->order('room.root_type asc')
			->select();
		$memberArr = '';
		foreach($memberList as $lss){
			$memberArr .= strlen($memberArr) ? ','. json_encode($lss['counts']) : '['. json_encode($lss['counts']);
			$mtotal += $ls['counts'];
		}
		
		$memberArr .= ']';
		
		$this->assign('mtotal', $total);
		//描述
		$content = "在住客户：". $counts ."间，出租率 ". round($counts/$total, 2)*100 ."%";
		$this->assign('houseArr', $houseArr);
		$this->assign('memberArr', $memberArr);
		$this->assign('content', $content);
		
		$this->display('chart/memberchart');
	}
	
	/**
	 * 每日签约数据统计
	 **/
	public function signmember(){
		$BeginDate = date('Y-m-01', strtotime(date("Y-m-d")));
		$lastday = date('d', strtotime("$BeginDate +1 month -1 day"));
		$this->assign('lastday', $lastday);
		//开始统计有效合同
		$count = D('Member')->getMemberListCount(array('visible' => 1));
		//开始统计每日的签约数据
		$list = M('member')->where(array(
			'visible' => 1,
			'FROM_UNIXTIME(btime,"%Y-%m")' => date('Y-m')
		))->field("count(*) as counts, FROM_UNIXTIME(btime,'%d') as days")
		->group("FROM_UNIXTIME(btime,'%Y-%m-%d')")
		->order('days asc')
		->select();
		//数值数组
		$valueArray = array();
		if(is_array($list) && count($list)){
			foreach($list as $ls){
				$valueArray[$ls['days']/1] = $ls['counts'];
			}
		}
		//组装数据
		$attrArr = '';
		for($i=1;$i<=$lastday;$i++){
			if(empty($valueArray[$i])){
				$attrArr .= strlen($attrArr) ? ',0' : '[0';
			}else{
				$attrArr .= strlen($attrArr) ? ','. json_encode($valueArray[$i]) : '['. json_encode($valueArray[$i]);
			}
			$nowcount += $valueArray[$i];
		}
		$attrArr .= ']';
		
		$this->assign('count', $count);
		$this->assign('attr', $attrArr);
		$this->assign('nowcount', $nowcount);
		$this->assign('today', date('d'));
		
		$this->display();
	}
	
	/**
	 * 每日退房数据统计
	 **/
	public function checkoutmember(){
		$BeginDate = date('Y-m-01', strtotime(date("Y-m-d")));
		$lastday = date('d', strtotime("$BeginDate +1 month -1 day"));
		$this->assign('lastday', $lastday);
		
		//获取退房总数
		$list = M('goback_log')->where(array(
			'typeid' => 0,
			'visible' => 1,
			'status' => 1,
			'FROM_UNIXTIME(createtime,"%Y-%m")' => date('Y-m'),
			'deposit' => array('lt', 0)
		))->field('DISTINCT roomnumber, count(*) as counts,nickname,sum(deposit) as deposit,FROM_UNIXTIME(createtime,"%d") AS days')
		->group('days asc')
		->select();
		//数值数组
		$valueArray = array();
		$count = 0;
		if(is_array($list) && count($list)){
			foreach($list as $ls){
				$valueArray[$ls['days']/1] = $ls['counts'];
				$count += abs($ls['deposit']);
			}
		}
		//组装数据
		$attrArr = '';
		for($i=1;$i<=$lastday;$i++){
			if(empty($valueArray[$i])){
				$attrArr .= strlen($attrArr) ? ',0' : '[0';
			}else{
				$attrArr .= strlen($attrArr) ? ','. json_encode($valueArray[$i]) : '['. json_encode($valueArray[$i]);
			}
			$nowcount += $valueArray[$i];
		}
		$attrArr .= ']';
		
		$this->assign('count', $count);
		$this->assign('attr', $attrArr);
		$this->assign('nowcount', $nowcount);
		$this->assign('today', date('d'));
		$this->display();
	}
	
	/**
	 * 每月固定费用统计
	 **/
	public function fixed(){
		//获取当前月
		$nowmonth = date('Y-m');
		$orderlist = M('costorder')->where(array(
			'month' => $nowmonth,
			'order_type' => 1,
			'status' => array('lt', 2)
		))->select();
		//定义变量
		$total = $account = $unacount = $count = $uncount = 0;
		//开始数据分析
		foreach($orderlist as $ls){
			if($ls['status'] == 1){
				$account += $ls['account'];
			}else{
				$unacount += $ls['account'];
				$uncount++;
			}
			$count++;
			$total += $ls['account'];
		}
		
		$this->assign('total', $total);
		$this->assign('count', $count);
		$this->assign('account', $account);
		$this->assign('unacount', $unacount);
		$this->assign('uncount', $uncount);
		
		$this->display();
	}
	
	/**
	 * 临时费用统计
	 **/
	public function temp(){
		//获取当前月
		$nowmonth = date('Y-m', strtotime("-1 month"));
		$orderlist = M('costorder')->where(array(
			'month' => $nowmonth,
			'order_type' => 2,
			'status' => array('lt', 2)
		))->select();
		//echo M('costorder')->_sql();
		//定义变量
		$total = $account = $unacount = $count = $uncount = 0;
		//开始数据分析
		foreach($orderlist as $ls){
			if($ls['status'] == 1){
				$account += $ls['account'];
			}else{
				$unacount += $ls['account'];
				$uncount++;
			}
			$count++;
			$total += $ls['account'];
		}
		
		$this->assign('total', $total);
		$this->assign('count', $count);
		$this->assign('account', $account);
		$this->assign('unacount', $unacount);
		$this->assign('uncount', $uncount);
		
		$this->display();
	}
}


   